(** Module Misc
 *
 *  Author: Jk
 *)

open Settings

exception MissingOption of string * ( Settings.tag Settings.t)

let getMandatoryStringOption conf name  = 
    match conf.get name with 
     | Some (Str v) -> v 
	 | _ -> raise (MissingOption (name,conf))
	 
let getOptionalStringOption conf name default =
    match conf.get name with 
	 | Some (Str v) -> v
	 | None -> default
	 | _ -> raise (Invalid_argument "It's Impossible!")

let genCommented s = "(* " ^ s ^ " *)"
let unsupported s = genCommented ("unsupported:" ^ s) ^ "()"

let stringToCharList s =
  let rec loop accu n = if n = 0 then accu else loop (String.make 1 s.[n - 1] ::accu) ( n - 1 ) in
  loop [] (String.length s)

let encodeName translateChar name = String.concat "" (List.map  translateChar (stringToCharList name))

(*let press = Str.global_replace (Str.regexp "\\( \\|\n\\|\r\\|\t\\)+") " "*)

(** useful functions *)

let (++) x f = f x

let o2l = function Some x -> [x] | None -> []

(* methods common for generation *)
module Generation = struct
  
let sprintf = Printf.sprintf

let getText = IL.Source.toString

let genOpt gen = function None -> "" | Some x -> gen x

let genOptText = genOpt getText

end
